package com.demo.youxuanmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.youxuanmall.common.Result;
import com.demo.youxuanmall.entity.Cart;
import com.demo.youxuanmall.entity.Product;
import com.demo.youxuanmall.entity.User;
import com.demo.youxuanmall.service.CartService;
import com.demo.youxuanmall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    
    @Autowired
    private ProductService productService;

    /**
     * 购物车页面
     */
    @GetMapping
    public String cartPage(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        return "cart/index";
    }

    /**
     * 获取购物车列表(数据)
     */
    @GetMapping(produces = "application/json")
    @ResponseBody
    public Result<List<Cart>> getCartData(HttpSession session) {
        try {
            log.info("获取购物车数据");
            User user = (User) session.getAttribute("user");
            if (user == null) {
                log.warn("用户未登录，无法获取购物车数据");
                return Result.error("请先登录");
            }
            log.info("获取用户{}的购物车数据", user.getId());
            return cartService.listUserCarts(user.getId());
        } catch (Exception e) {
            log.error("获取购物车列表失败", e);
            return Result.error("获取失败，请重试");
        }
    }

    /**
     * 获取购物车列表
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<List<Cart>> getCartList(HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.listUserCarts(user.getId());
        } catch (Exception e) {
            log.error("获取购物车列表失败", e);
            return Result.error("获取失败，请重试");
        }
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    @ResponseBody
    public Result<Cart> addToCart(@RequestParam Long productId,
                                   @RequestParam Integer quantity,
                                   HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.addToCart(user.getId(), productId, quantity);
        } catch (Exception e) {
            log.error("添加购物车失败", e);
            return Result.error("添加失败，请重试");
        }
    }

    /**
     * 更新购物车商品数量
     */
    @PostMapping("/update")
    @ResponseBody
    public Result<Cart> updateCartItem(@RequestParam Long id,
                                        @RequestParam Integer quantity,
                                        HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.updateQuantity(user.getId(), id, quantity);
        } catch (Exception e) {
            log.error("更新购物车失败", e);
            return Result.error("更新失败，请重试");
        }
    }

    /**
     * 更新购物车商品选中状态
     */
    @PostMapping("/update/checked")
    @ResponseBody
    public Result<Cart> updateCartItemChecked(@RequestParam Long id,
                                               @RequestParam Integer checked,
                                               HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.updateChecked(user.getId(), id, checked);
        } catch (Exception e) {
            log.error("更新购物车选中状态失败", e);
            return Result.error("更新失败，请重试");
        }
    }

    /**
     * 删除购物车商品
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result<Void> removeCartItem(@RequestParam Long id,
                                        HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.deleteCart(user.getId(), id);
        } catch (Exception e) {
            log.error("删除购物车商品失败", e);
            return Result.error("删除失败，请重试");
        }
    }

    /**
     * 清空购物车
     */
    @PostMapping("/clear")
    @ResponseBody
    public Result<Boolean> clearCart(HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.clearCart(user.getId());
        } catch (Exception e) {
            log.error("清空购物车失败", e);
            return Result.error("清空失败，请重试");
        }
    }

    /**
     * 全选/取消全选
     */
    @PostMapping("/select/all")
    @ResponseBody
    public Result<Void> selectAll(@RequestParam Integer checked,
                                   HttpSession session) {
        try {
            User user = (User) session.getAttribute("user");
            if (user == null) {
                return Result.error("请先登录");
            }
            return cartService.selectAllOrNone(user.getId(), checked);
        } catch (Exception e) {
            log.error("更新购物车全选状态失败", e);
            return Result.error("更新失败，请重试");
        }
    }
} 